<?php

// 获取某套装的vr图片管理默认层
function test_vr_get_default($json)
{
	$tzid = get_jsonValue($json,"tzid") ;
		
	$ret = array() ;
	$list = sql_fetch_rows("select * from taozhuang_vr_pos where `tzid` = '$tzid' and `order` = '0' order by `pos` asc") ;
	
	foreach ($list as &$im){
		test_util_set_IMAGE($im , $im['img_id']) ;
	}
	$ret['vrList'] = $list ;
	return $ret ;	
}

// 添加新的层数图片
function test_vr_add_pic($json)
{
	$tzid = get_jsonValue($json,"tzid") ;
	$imgid = get_jsonValue($json,"imgid") ;
	
	$newpos = intval(sql_fetch_one_cell("select max(`pos`) from taozhuang_vr_pos where `tzid` = '$tzid'")) + 1 ;
	
	$newid = sql_insert("INSERT INTO `taozhuang_vr_pos` (`tzid`, `img_id`,`order`,`pos`) VALUES ('$tzid','$imgid', '0' ,'$newpos')") ;
	
	return test_vr_get_default($json) ;
}

// 插入新的层数图片
function test_vr_ins_pic($json)
{
	$tzid = get_jsonValue($json,"tzid") ;
	$imgid = get_jsonValue($json,"imgid") ;
	$inspos = get_jsonValue($json,"inspos") ;
	
	$maxpos = intval(sql_fetch_one_cell("select max(`pos`) from taozhuang_vr_pos where `tzid` = '$tzid'")) ;
	for($i = $maxpos ; $i >= $inspos ; -- $i){
		sql_query("update `taozhuang_vr_pos` set `pos` = $i + 1 where `tzid` = '$tzid' and `pos` = '$i'") ;		
	}
	
	$newid = sql_insert("INSERT INTO `taozhuang_vr_pos` (`tzid`, `img_id`,`order`,`pos`) VALUES ('$tzid','$imgid', '0' ,'$inspos')") ;
	
	return test_vr_get_default($json) ;
}

// 删除某层所有图片
function test_vr_del_pos_pic($json)
{
	$tzid = get_jsonValue($json,"tzid") ;
	$delpos = get_jsonValue($json,"delpos") ;
	
	sql_query("delete from `taozhuang_vr_pos` where  `tzid` = '$tzid'  and `pos` = '$delpos' ") ;
	
	$maxpos = intval(sql_fetch_one_cell("select max(`pos`) from taozhuang_vr_pos where `tzid` = '$tzid'")) ;
	
	for($i = $delpos + 1 ; $i <= $maxpos ;  ++ $i){
		sql_query("update `taozhuang_vr_pos` set `pos` = $i - 1 where `tzid` = '$tzid' and `pos` = '$i'") ;
	}
	
	return test_vr_get_default($json) ;
}


// 获取某层的可替换资源
function test_vr_get_pos_order($json)
{
	$tzid = get_jsonValue($json,"tzid") ;
	$getpos = get_jsonValue($json,"getpos") ;
	
	$ret = array() ;
	$list = sql_fetch_rows("select * from taozhuang_vr_pos where `tzid` = '$tzid' and `pos` = '$getpos' order by `order` asc") ;
	
	foreach ($list as &$im){
		test_util_set_IMAGE($im , $im['img_id']) ;
	}
	$ret['vrList'] = $list ;
	return $ret ;		
}
// 添加某层可替换资源
function test_vr_add_order($json)
{
	$tzid = get_jsonValue($json,"tzid") ;
	$getpos = get_jsonValue($json,"getpos") ;
	$imgid = get_jsonValue($json,"imgid") ;
	
	$maxorder = intval(sql_fetch_one_cell("select max(`order`) from taozhuang_vr_pos where `tzid` = '$tzid' and `pos` = '$getpos'")) + 1 ;
	
	$newid = sql_insert("INSERT INTO `taozhuang_vr_pos` (`tzid`, `img_id`,`order`,`pos`) VALUES ('$tzid','$imgid', '$maxorder' ,'$getpos')") ;
	
	return test_vr_get_pos_order($json) ;
}
// 删除某层可替换资源
function test_vr_del_order($json)
{
	$tzid = get_jsonValue($json,"tzid") ;
	$getpos = get_jsonValue($json,"getpos") ;
	$order = get_jsonValue($json,"order") ;
	if (intval($order) == 0){
		$ret = array() ;
		$ret['status'] = -1 ;
		$ret['message'] = "第一个无法删除" ;
		return $ret ;
	}
	
	sql_query("delete from `taozhuang_vr_pos` where  `tzid` = '$tzid'  and `pos` = '$getpos' and `order` = '$order' ") ;
	
	$maxorder = intval(sql_fetch_one_cell("select max(`order`) from taozhuang_vr_pos where `tzid` = '$tzid' and `pos` = '$getpos'")) ;
	
	for($i = $order + 1 ; $i <= $maxorder ;  ++ $i){
		sql_query("update `taozhuang_vr_pos` set `order` = $i - 1 where `tzid` = '$tzid' and `pos` = '$i' and `order` = '$i'") ;
	}
	
	return test_vr_get_pos_order($json) ;
}

// 某可替换层设置为默认值
function test_vr_setdefault_order($json)
{
	$tzid = get_jsonValue($json,"tzid") ;
	$getpos = get_jsonValue($json,"getpos") ;
	$order = get_jsonValue($json,"order") ;
	if (intval($order) == 0){
		$ret = array() ;
		$ret['status'] = -1 ;
		$ret['message'] = "已经是默认了" ;
		return $ret ;
	}
	
	sql_query("update `taozhuang_vr_pos` set `order` = -1 where `tzid` = '$tzid' and `pos` = '$getpos' and `order` = '0'") ;
	sql_query("update `taozhuang_vr_pos` set `order` = 0 where `tzid` = '$tzid' and `pos` = '$getpos' and `order` = '$order'") ;
	sql_query("update `taozhuang_vr_pos` set `order` = $order where `tzid` = '$tzid' and `pos` = '$getpos' and `order` = '-1'") ;
	
	return test_vr_get_pos_order($json) ;
}

// 获取
function test_vr_get_exch($json)
{
	$id = get_jsonValue($json,"id") ;
	$listtz = sql_fetch_rows("select * from `taozhuang_vr_exch` where `tzid` = '$id'");
	
	$ret = array() ;
	$ret['list'] = $listtz ;
	return $ret ;		
}
// 更新
function test_vr_update_exch($json)
{
	$id = get_jsonValue($json,"id") ;
	$x = get_jsonValue($json,"x") ;
	$y = get_jsonValue($json,"y") ;
	
	sql_query("update taozhuang_vr_exch set `x` = '$x' , `y` = '$y' where `id` = '$id'") ;
	$ret = array() ;
	return $ret ;
}
// 添加
function test_vr_add_exch($json)
{
	$ret = array() ;
	$id = get_jsonValue($json,"id") ;
	$pos = intval(get_jsonValue($json,"pos")) ;
	if ($pos <= 0){
		$ret['status'] = -1 ;
		$ret['message'] = "请选择层数" ;
		return $ret ;		
	}

	if (intval(sql_fetch_one_cell("select `id` from `taozhuang_vr_exch` where `tzid` = '$id' and `pos` = '$pos'")) > 0){
		$ret['status'] = -1 ;
		$ret['message'] = "POS($pos)层已经存在" ;
		return $ret ;
	}
	
	$x = rand(100,300) ;
	$y = rand(150,350) ;
	$newid = sql_insert("INSERT INTO `taozhuang_vr_exch` (`tzid`, `pos`,`x`,`y`) VALUES ('$id','$pos', '$x' ,'$y')") ;
	
	return test_vr_get_exch($json) ;
}
// 添加跳转的url
function test_vr_add_url_exch($json)
{
	$ret = array() ;
	$id = get_jsonValue($json,"id") ;
	$gotourl = get_jsonValue($json , "goto_url") ;
	
	$pos = intval(sql_fetch_one_cell("select min(pos) from `taozhuang_vr_exch` where `tzid` = '$id'")) - 1 ;
	if ($pos >= 0) $pos = -1 ;
	
	$x = rand(100,300) ;
	$y = rand(150,350) ;
	$newid = sql_insert("INSERT INTO `taozhuang_vr_exch` (`tzid`, `pos`,`x`,`y`,`goto_url`) VALUES ('$id','$pos', '$x' ,'$y','$gotourl')") ;
	
	return test_vr_get_exch($json) ;
}
// 修改跳转的Url
function test_vr_mod_url_exch($json)
{
	$id = get_jsonValue($json,"mod_id") ;
	
	if (intval(sql_fetch_one_cell("select `id` from `taozhuang_vr_exch` where `id` = '$id'")) <= 0){
		$ret = array() ;
		$ret['status'] = -1 ;
		$ret['message'] = "POS($pos)层已经不存在了" ;
		return $ret ;
	}
	
	$gotourl = get_jsonValue($json , "goto_url") ;
	sql_query("update `taozhuang_vr_exch` set `goto_url` = '$gotourl' where `id` = '$id'") ;
	
	return test_vr_get_exch($json) ;
}
// 修改跳转的图片
function test_vr_mod_img_exch($json)
{
	$id = get_jsonValue($json,"mod_id") ;
	
	if (intval(sql_fetch_one_cell("select `id` from `taozhuang_vr_exch` where `id` = '$id'")) <= 0){
		$ret = array() ;
		$ret['status'] = -1 ;
		$ret['message'] = "POS($pos)层已经不存在了" ;
		return $ret ;
	}
	
	$gotoimg = get_jsonValue($json , "goto_img") ;
	sql_query("update `taozhuang_vr_exch` set `goto_img` = '$gotoimg' where `id` = '$id'") ;
	
	return test_vr_get_exch($json) ;
}
// 删除
function test_vr_del_exch($json)
{
	$id = get_jsonValue($json,"del_id") ;
	
	if (intval(sql_fetch_one_cell("select `id` from `taozhuang_vr_exch` where `id` = '$id'")) <= 0){
		$ret = array() ;
		$ret['status'] = -1 ;
		$ret['message'] = "POS($pos)层已经不存在了" ;
		return $ret ;
	}
	
	sql_query("delete from taozhuang_vr_exch where `id` = '$id'") ;
	
	return test_vr_get_exch($json) ;
}

// plzh
function test_global_zuhe($arr){
    if(count($arr) >= 2){
        $tmparr = array();
        $arr1 = array_shift($arr);
        $arr2 = array_shift($arr);
        foreach($arr1 as $k1 => $v1){
            foreach($arr2 as $k2 => $v2){
                $tmparr[] = $v1.",".$v2;
            }
        }
        array_unshift($arr, $tmparr);
        $arr = test_global_zuhe($arr);
    }else{
        return $arr;
    }
    return $arr;
}
	
// 获取某套装的VR数据	
function test_vr_get_big_buffer($json)
{
	$tzid = get_jsonValue($json,"id") ;
	
	$ret = array() ;
	
	$bufflist = sql_fetch_rows("select * from taozhuang_vr_buffer where  `tzid` = '$tzid'") ;
	
	foreach($bufflist as &$bb){
		$bb['img_id_fileName'] = $tzid."_".$bb['id'].".jpg" ;
		$bb['img_id_url'] = test_util_get_IMAGE_url($bb['img_id']) ;
		$bb['img_b_url'] = test_util_get_IMAGE_url($bb['img_b']) ;
		$bb['img_d_url'] = test_util_get_IMAGE_url($bb['img_d']) ;
		$bb['img_f_url'] = test_util_get_IMAGE_url($bb['img_f']) ;
		$bb['img_l_url'] = test_util_get_IMAGE_url($bb['img_l']) ;
		$bb['img_r_url'] = test_util_get_IMAGE_url($bb['img_r']) ;
		$bb['img_u_url'] = test_util_get_IMAGE_url($bb['img_u']) ;
	}
	
	$ret['list'] = $bufflist ;
	
	return $ret ;
}
// 生成某个套装的VR所有大图数据
function test_vr_create_big($json)
{
	$tzid = get_jsonValue($json,"id") ;
	
	$vrlist = sql_fetch_rows("select * from taozhuang_vr_buffer where `tzid` = '$tzid'") ;
	if (is_array($vrlist) == false || count($vrlist) > 0)
	{
		$ret = array() ;
		$ret['status'] = -1 ;
		$ret['message'] = "此套装已经生成过了,如需再次生成,请先清除." ;
		return $ret ;
	}
	
	$vrlist = sql_fetch_rows("select * from taozhuang_vr_pos where `tzid` = '$tzid' order by `pos` , `order` ") ;
	if (is_array($vrlist) == false || count($vrlist) == 0)
	{
		$ret = array() ;
		$ret['status'] = -1 ;
		$ret['message'] = "此套装没有任何数据,无法生成" ;
		return $ret ;
	}
	

	$posarr = array() ;	
	foreach($vrlist as $v){
		$id = intval($v['id']) ;
		$img_id = intval($v['img_id']) ;
		$pos = intval($v['pos']) ;
		$order = intval($v['order']) ;

		if (array_key_exists($pos , $posarr)){
			$posarr[$pos][] = $v ;
		}else{
			$posarr[$pos] = array() ;
			$posarr[$pos][] = $v ;
		}
	}
	
	$vl_1 = array() ;
	foreach($posarr as $v)
	{
		$aa = array() ;
		foreach($v as $v1){
			$aa[] = $v1['id']."|".$v1['img_id'] ;
		}
		$vl_1[] = $aa ;
	}

	$listres = test_global_zuhe($vl_1) ;

	$processList = array() ;
	foreach($listres[0] as $val){
		$pos_list = "" ; 
		$img_list = "" ;
		$dh = "" ;
		$arr = explode(",",$val) ;
		foreach($arr as $v){
			$ps = explode("|",$v) ;
			
			$pos_list .= $dh.$ps[0] ;	
			$img_list .= $dh.$ps[1] ;
			
			$dh = "," ;
		}
	
		$newid = sql_insert("insert into taozhuang_vr_buffer(`tzid` , `pos_list` , `img_list`) values ('$tzid' , '$pos_list' , '$img_list')") ;
	}

	return test_vr_get_big_buffer($json) ;
}
// 删除某个套装的所有VR大图及小图数据
function test_vr_delete_all($json)
{
	$tzid = get_jsonValue($json,"id") ;
	
	$vrlist = sql_fetch_rows("select * from taozhuang_vr_buffer where `tzid` = '$tzid'") ;
	if (is_array($vrlist) == false || count($vrlist) == 0)
	{
		$ret = array() ;
		$ret['status'] = -1 ;
		$ret['message'] = "此套装已经清除过了.无需再次清理" ;
		return $ret ;
	}
	
	foreach($vrlist as $vr)
	{
		$vvv = array() ;
		$vvv[] = $vr['img_id'] ;
		$vvv[] = $vr['img_b'] ;
		$vvv[] = $vr['img_d'] ;
		$vvv[] = $vr['img_f'] ;
		$vvv[] = $vr['img_l'] ;
		$vvv[] = $vr['img_r'] ;
		$vvv[] = $vr['img_u'] ;
		
		foreach($vvv as $id){
			if ($id > 0){
				$img = sql_fetch_one("select * from sys_image where `id` = '$id'") ;
				if (is_array($img) == true && intval($img['id']) == intval($id)){
					sql_query("delete from sys_image where `id` = '$id'") ;
					if (sql_check("select * from sys_image where `id` = '$id'") == false){				
						$file_path = $GLOBALS['upload_Path'] . $img['url'] ;
						unlink($file_path) ;
					}
				}
			}
		}
	}
	
	sql_query("delete from taozhuang_vr_buffer where `tzid` = '$tzid'") ;
	
	return test_vr_get_big_buffer($json) ;
}
// 执行外部脚本命令批量生成大图
function test_vr_create_all_big_img($json)
{
	$tzid = intval(get_jsonValue($json,"id")) ;
	if ($tzid <= 0){
		$ret = array() ;
		$ret['status'] = -1 ;
		$ret['message'] = "套装id错误" ;
		return $ret ;
	}
	
	
	$info = sql_fetch_one("select * , unix_timestamp() as NowTM  from taozhuang_vr_temp where tzid = '$tzid' and  `type` = 1") ;
	if (is_array($info) == false || intval($info['tzid']) != $tzid)
	{
		$et = 1800 ;  // 半小时都没生成好,下次可以重新生成 
		sql_insert("INSERT INTO `taozhuang_vr_temp` (`tzid`, `starttime`, `state`, `endtime`, `type`, `process`) VALUES ('$tzid', unix_timestamp(), '0', unix_timestamp() + $et, '1', '0%')") ;
		$info = sql_fetch_one("select * , unix_timestamp() as NowTM from taozhuang_vr_temp where tzid = '$tzid' and  `type` = 1") ;
	}
	
	
	if (is_array($info) == false || intval($info['tzid']) != $tzid)
	{
		$ret = array() ;
		$ret['status'] = -1 ;
		$ret['message'] = "记录生成失败" ;
		return $ret ;
	}
	if ($info['state'] == 1 && $info['NowTM'] < $info['endtime'])
	{
		$ret = array() ;
		$ret['status'] = -1 ;
		$ret['message'] = "脚本正在生成中" ;
		return $ret ;
	}
	
	$rtval = -2 ;
	$runfile = dirname(__FILE__)."/core_vr_tz.sh tzid $tzid >> a.txt &" ;
	$rnts = system($runfile , $rtval) ;
	
	$ret = array() ;
	$ret['status'] = $rtval ;
	if ($rtval == 0){
		$ret['message'] = "执行成功.$rtval" ;
	}elseif ($rtval == 101){
		$ret['message'] = "执行失败,有其他套装VR脚本运行中,请稍候重试.$rtval" ;
	}else{
		$ret['message'] = "执行失败,其他原因.$rtval , $runfile , $rnts" ;
	}
	return $ret ;
}
function test_vr_get_create_all_big_process($json)
{
	$tzid = intval(get_jsonValue($json,"id")) ;
	if ($tzid <= 0){
		$ret = array() ;
		$ret['status'] = -1 ;
		$ret['message'] = "套装id错误" ;
		return $ret ;
	}
	
	$info = sql_fetch_one("select * , unix_timestamp() as NowTM from taozhuang_vr_temp where tzid = '$tzid' and  `type` = 1") ;
	$ret = array() ;
	$ret['info'] = $info ;
	return $ret ;
}
function test_vr_get_zip_tz_big_img($json)
{
	$tzid = get_jsonValue($json,"id") ;
	
	$ret = array() ;
	
	$bufflist = sql_fetch_rows("select * from taozhuang_vr_buffer where  `tzid` = '$tzid'") ;
	
	$savename = "" ;
	$returnurl = "" ;
	$tarlist = "" ;
	
	foreach($bufflist as &$bb){
		$tarlist .= " " . basename(test_util_get_IMAGE_Addr($bb['img_id'])) ;	
		if (strlen($savename) == 0){
			$savename = test_util_get_IMAGE_Addr($bb['img_id']) ;
			$returnurl = test_util_get_IMAGE_url($bb['img_id']) ;
		}
	}
	
	$savename .= ".tar.gz" ;	
	$returnurl .= ".tar.gz" ;
	
	$dir = dirname($savename) ;
	
	$runfile = "tar zcvf $savename -C $dir  $tarlist" ;
	$rnts = system($runfile) ;
	
	$ret['url'] = $returnurl ;
	return $ret ;
}
// 生成一张大图.
function test_vr_create_one_imgpic($json)
{
	$id = get_jsonValue($json,"id") ;
	
	$buffone = sql_fetch_one("select * from taozhuang_vr_buffer where `id` = '$id'") ;
	
	if (is_array($buffone) == false || intval($buffone['id']) != $id)
	{
		$ret = array() ;
		$ret['status'] = -1 ;
		$ret['message'] = "未找到生成的信息" ;
		return $ret ;
	}
	
	$tzid = $buffone['tzid'] ;
	$img_list = $buffone['img_list'] ;
	
	$old_img = $buffone['img_id'] ;
	
	/**
	 * 创建并生成大图
	 */
	$nm = $tzid."_".$id.".jpg" ;
	$saveinfo = img_set_imglists_toOne($img_list , $nm , $old_img) ;
	sql_query("update taozhuang_vr_buffer set `img_id` = '{$saveinfo[0]}' where `id` = '$id'") ;
	
	$ret = array() ;
	$ret['imgid'] = $saveinfo[0] ;
	$ret['url'] = $saveinfo[2] ;
	return $ret ;
}
// 更改VR对应的单品
function test_vr_setpos_item_id($json)
{
	$id = get_jsonValue($json,"id") ;
	$item_id = get_jsonValue($json,"item_id") ;
	
	sql_query("update `taozhuang_vr_pos` set `item_id` = $item_id where `id` = '$id'") ;
	
	$ret = array() ;
	$ret['message'] = "修改成功" ;
	return $ret ;
}



?>